package main.java.com.zheng.coderepo.permission;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by zhangchaozheng on 15-2-11.
 */
public class Permission {

    private Integer permissionId;

    private String permissionName;

    private String permissionInfo;

    public Integer getPermissionId() {
        return permissionId;
    }

    public void setPermissionId(Integer permissionId) {
        this.permissionId = permissionId;
    }

    public String getPermissionName() {
        return permissionName;
    }

    public void setPermissionName(String permissionName) {
        this.permissionName = permissionName;
        initParts(permissionName);
    }

    public String getPermissionInfo() {
        return permissionInfo;
    }

    public void setPermissionInfo(String permissionInfo) {
        this.permissionInfo = permissionInfo;
    }

    protected static final String WILDCARD_TOKEN = "*";
    protected static final String PART_DIVIDER_TOKEN = ":";
    protected static final String SUBPART_DIVIDER_TOKEN = ",";

    public Permission() {

    }

    private List<Set<String>> parts;

    public void setParts(List<Set<String>> parts) {
        this.parts = parts;
    }

    public void initParts(String permissionName) {
        if (permissionName == null || permissionName.trim().length() == 0) {
            return;
        }

        permissionName = permissionName.trim().toLowerCase();

        List<String> parts = Arrays.asList(permissionName.split(PART_DIVIDER_TOKEN));

        this.parts = new ArrayList<Set<String>>();
        for (String part : parts) {
            Set<String> subparts = new HashSet<String>(Arrays.asList(part.split(SUBPART_DIVIDER_TOKEN)));

            if (!subparts.isEmpty()) {
                this.parts.add(subparts);
            }
        }
    }

    public List<Set<String>> getParts() {
        return this.parts;
    }

    public boolean isPermitted(String permissionName) {
        Permission otherPermission = new Permission();
        otherPermission.setPermissionName(permissionName);
        List<Set<String>> otherParts = otherPermission.getParts();
        int i = 0;
        for (Set<String> otherPart : otherParts) {
            if (getParts().size() - 1 < i) {
                return true;
            } else {
                Set<String> part = getParts().get(i);
                if (!part.contains(WILDCARD_TOKEN) && !part.containsAll(otherPart)) {
                    return false;
                }
                i++;
            }
        }

        for (; i < getParts().size(); i++) {
            Set<String> part = getParts().get(i);
            if (!part.contains(WILDCARD_TOKEN)) {
                return false;
            }
        }

        return true;
    }

    public static void main(String[] args) throws ParseException {
        Permission permission = new Permission();
        permission.setPermissionName("op:permission:*");
        System.out.println(permission.isPermitted("op:permission"));
        System.out.println(System.currentTimeMillis());
        System.out.println(new SimpleDateFormat("yyyyMMddHHmmss").parse("20160313000000").getTime());
    }
}
